var comm = {
	// 把json转化成name=zhangsan&age=18字符串
	stringify: function (json) {
		var str = "";
		for (var i in json) {
			str += i + "=" + json[i] + "&";
		}
		return str.slice(0, -1);
	},
	// 把不是马上执行而且执行时间不确定的函数叫做回调函数
	/**
	 * ajax 请求函数
	 *
	 * @param type 表示类型 如：post|get
	 * @param url 要访问的地址
	 * @param params Json类型 表示要发送的参数
	 * @param 成功后的回调函数 如果不传表示不执行
	 * @param 失败后执行的函数
	 */
	ajax: function (type, url, params, callback, errback) {
		var xhr = null;
		//表IE
		if (window.XMLHttpRequest) {
			xhr = new XMLHttpRequest();
		} else {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
		if (type == "get") {
			xhr.open(type, url + "?" + this.stringify(params), true);
			xhr.send();
		} else {
			xhr.open(type, url, true);
			xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			//json转换成name=张三&age=1
			xhr.send(this.stringify(params));
		}

		xhr.onreadystatechange = function () {
			// 表示请求已完成
			if (xhr.readyState == 4) {

				if (xhr.status == 200) {
					if (callback) {
						callback(xhr.responseText);
					}
				} else {
					if (errback) {
						errback();
					}
				}

			}
		}


	},
	setCookie : function(key,value,json){
		if (typeof value == "object") {
			value = JSON.stringify(value);
		} 

		var str = encodeURIComponent(key) + "=" + encodeURIComponent(value);
		if (json) {
			if (typeof json.expires == "number") {
				var date = new Date();
				date.setDate(date.getDate() + json.expires)
				str += ";expires=" + date;
			}
			if (json.path) {
				str += ";path=" + json.path;
			}
			if (json.domain) {
				str += ";domain=" + json.domain;
			}
			if (json.secure) {
				str += ";secure";
			}
		}
		
		document.cookie = str;
	},

	// //document.cookie name=zhangsan; age=18; sex=man
	getCookie : function(key){

		key = encodeURIComponent(key);
		//[name=zhangsan, age=18; sex=man]

		var arr= document.cookie.split("; ");
		for (var i = 0; i < arr.length; i++) {
			//[name,zhangsan][age,18]
			var subArr = arr[i].split("=");
			if (subArr[0] == key){
				return decodeURIComponent(subArr[1]);
			}
		}
	},

	removeCookie :function(key){
		this.setCookie(key,null,{expires:-1});
	},

	trimLeft : function(str){
		return str.replace(/^\s+/, "");
	},
	trimRight : function(str) {
		return str.replace(/\s+$/, "");
	},
	trim : function(str){
		return str.replace(/^\s+/, "").replace(/\s+$/, "");
	},
	regExp : {
		email : function(str){
			return /^\w+@\w+(\.\w+)+$/.test(str);
		}
	},

	getAttr : function(ele, attr){
		if (getComputedStyle) {
			return parseInt(getComputedStyle(ele)[attr]);
		}
		return parseInt(ele.currentStyle[attr]);
	},
	// animate("box",{left:"200px", top : "100px"});
	animate : function(id, attrs){
		var ele = document.getElementById(id);
		var that = this;
		var flag = true;
		var timer = setInterval(function(){
			for (var i in attrs) {
				var speed = Math.floor((parseInt(attrs[i]) - that.getAttr(ele, i)) / 6);
				ele.style[i] = that.getAttr(ele, i) + speed + "px";
				if (speed < 0.1) {
					for (var j in attrs){
						ele.style[j] = attrs[j];
					}
					clearInterval(timer);
				}
			}

		}, 30);
	},

	animate2 : function(id, attrs){
		var ele = document.getElementById(id);
		var that = this;
		ele.timerList = ele.timerList || {};
		ele.flagList = {};
		for (var i in attrs) {
			 clearInterval(ele.timerList[i]);
			(function(i){
				ele.timerList[i] = setInterval(function () {
					console.log(i);
					var speed = Math.floor((parseInt(attrs[i]) - that.getAttr(ele, i)) / 6);
					ele.style[i] = that.getAttr(ele, i) + speed + "px";
					ele.flagList = false;
					if (speed <= 0.01) {
						ele.style[i] = attrs[i];
						ele.flagList[i] = true;
						clearInterval(ele.timerList[i]);
					}
				}, 30)
			})(i);
		}
	},

	/**
		 *
		 * @param node 对象
		 * @param style 输入样式
		 * 		@example {left: 400, top:100}
         */
		 lanimate : function(node, style, handler) {
			// 如果对象没有该属性会出undefined，如果对象没有该属性，
			// 再往该对象取属性直接报错相当于undefined.left
			if (!node.timerList) {
				node.timerList = {};
			}

			node.isMoving = true;
			//{left: 400, top:100}
			for (var type in style) {

				//通过函数保留变量的值
				this.lanimateCore(node, style, type, handler);

			}
		},

		 lanimateCore : function(node, style, type, handler){
			// 如 left 的值
			var target = style[type];
			// 使用定时器 最好每次使用之前先清除相应的定时器，这样可以保证每次只执行一个定时器
			//【注】
			// 避免使用全局变量
//			node = {
//				timerList: {
//					left : 1;
//				}
//			}
			var that = this;
			clearInterval(node.timerList[type]);
			node.timerList[type] = setInterval(function(){
				var speed = Math.floor((target - parseInt(that.lcss(node, type))) / 6);
				//ball.style.left = ball.offsetLeft + speedX + "px";
				var currentPos = parseInt(that.lcss(node, type));
				node.style[type] = currentPos + speed + "px";
				node.isMoving = true;
				if (Math.abs(speed) < 0.1) {
					node.style[type] = target + "px";
					clearInterval(node.timerList[type]);
					node.isMoving = false;

					if (handler) {
						handler();
					}
				}
			}, 30);
			//console.log("后" + node.timerList.left);
		},
		//css($("#ball"), "left"); return left值
		// 三元运算符或者叫三目运算符
		lcss : function(node, type){
			return node.currentStyle ? node.currentStyle[type]
					: getComputedStyle(node, false)[type];
		},
		
		getClassName : function getElementsByClsName(clsName){
			
		   if (document.getElementsByClassName) {
		      return document.getElementsByClassName(clsName);
		   }
		   var all = (document || element).getElementsByTagName("*");
		   var arr = [];
		   var j = 0;
		   for (var i = 0; i < all.length; i++) {
		      if (all[i].className == clsName) {
		         arr[j++] = all[i];
		      }
		   }
		   return  arr;

}

function $(id){
	return document.getElementById(id);
}

